//
//  XMWithdrawListViewController.m
//  XunMi
//
//  Created by MacBook on 2024/3/14.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMWithdrawListViewController.h"
#import "XMWithdrawTableViewCell.h"
#import "XMWithdrawModel.h"

@interface XMWithdrawListViewController ()
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray <XMWithdrawModel *> *dataArray;

@end

@implementation XMWithdrawListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
    [self withdrawList];
    [self setupRefresh];
}

#pragma mark - refresh
- (void)setupRefresh {
    XMWeakSelf(self);
    self.tableView.mj_header = [XMRefreshNormalHeader headerWithRefreshingBlock:^{
        XMStrongSelf(self);
        [self withdrawListRefresh];
    }];
    self.tableView.mj_footer = [XMRefreshBackFooter footerWithRefreshingBlock:^{
        XMStrongSelf(self);
        self.page += 1;
        [self withdrawList];
    }];
    self.tableView.mj_footer.hidden = YES;
}

- (void)withdrawListRefresh {
    self.page = 1;
    [self.dataArray removeAllObjects];
    [self withdrawList];
}

#pragma mark -
- (void)withdrawList {
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"page_no":@(self.page),
        @"page_size":@(10)
    }.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/money/moneyRecord" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        [self.tableView hideStatusView];
        if (tip.success) {
            [MBProgressHUD hideHUD];
            NSArray *record = [result safe_objectForKey:@"record"];
            NSArray *array = [XMWithdrawModel mj_objectArrayWithKeyValuesArray:record];
            [self.dataArray addObjectsFromArray:array];
            if (self.dataArray.count == 0) {
                [self.tableView showStatusView:(YHViewStatusTypeEmptyData)];
                self.tableView.statusTitleLabel.text = @"您还没有提现记录哦～";
            }
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        if (tip.loadMore) {
            [self.tableView.mj_footer endRefreshing];
        } else {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

#pragma mark - 初始化变量
- (void)initVariables {
    self.dataArray = [NSMutableArray array];
    self.page = 1;
    [self tableViewRegisterNibWithNibName:kStringFromClass(XMWithdrawTableViewCell)];
}

#pragma mark - setupSubViews
- (void)setupSubviews {
    [self.navLeft2Btn setTitle:@"提现记录" forState:UIControlStateNormal];
}

#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 88;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMWithdrawTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMWithdrawTableViewCell class])];
    if (self.dataArray.count > 0) {
        cell.model = [self.dataArray safe_objectAtIndex:indexPath.row];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

@end
